import json

import scrapy

from dangdang_scrapy.items import DangdangScrapyItem


class DangdangSpider(scrapy.Spider):
    name = "dangdang"
    # def __init__(self, *args, **kwargs):
    #     super().__init__(*args, **kwargs)
    #     self.email_extension = EmailNotificationExtension(self.settings)
    #     self.email_extension.start_sending_emails(self)
    start_urls = [
        "https://e.dangdang.com/media/api.go?action=mediaCategoryLeaf&promotionType=1&deviceSerialNo=html5&macAddr"
        "=html5&channelType=html5&permanentId=20250414211611217215734391672352987&returnType=json&channelId=70000"
        "&clientVersionNo=6.8.0&platformSource=DDDS-P&fromPlatform=106&deviceType=pconline&token=&start=21&end=41"
        "&category=WY1&dimension=dd_sale&order=0"]

    def start_requests(self):
        header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0',
        }
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse, headers=header)

    def parse(self, response):

        json_book_list = json.loads(response.text)
        for books in json_book_list['data']['saleList']:
            for book in books['mediaList']:
                book_name = book['authorPenname']
                coverPic = book['coverPic']
                descs = book['descs']
                book_object = DangdangScrapyItem(title=book_name, img_url=coverPic, detail_url=descs)
                yield book_object
